package com.tqfframe.rabbitmq.listener.delay;

import com.rabbitmq.client.Channel;
import com.tqfframe.rabbitmq.constants.RabbitConstants;
import com.tqfframe.rabbitmq.entity.TestEntity;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * 死性消息队列监听
 * @author: Qifeng Tang
 * @date: 2022/8/31 11:43
 * @Version 1.0
 **/
@Component
public class DelayReceiver {

    /**
     * 接收字符串
     * @param channel
     */
//    @RabbitListener(queues = RabbitConstants.DELAY_QUEUE)
//    @RabbitHandler
    public void receive(@Payload String content, Message message, Channel channel) throws IOException {
        System.out.println("我消费了延迟队列消息"+content);
        try {
            // 消息的可定确认，第二个参数如果为true将一次性确认所有小于deliveryTag的消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            // 直接拒绝消费该消息，后面的参数一定要是false，否则会重新进入业务队列，不会进入死信队列
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
        }
    }

    /**
     * 接收对象
     */
    @RabbitListener(queues = RabbitConstants.DELAY_QUEUE)
    public void receive2(@Payload TestEntity testEntity, @Headers Map<String, Object> headers, Channel channel) {
        System.out.println("我开始消费了。"+testEntity.toString());
    }

    /**
     * 接收字符串
     */
//    @RabbitListener(queues = RabbitConstants.DELAY_QUEUE)
    public void receive3(@Payload String content, @Headers Map<String, Object> headers, Channel channel) throws IOException {
        System.out.println("我消费了延迟队列消息"+content);
    }


}
